package com.token.serviceImpl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.token.bean.Jin;
import com.token.bean.User;
import com.token.bean.layuiBean.ResultPage;
import com.token.bean.layuiBean.VoPage;
import com.token.mapper.UserMapper;
import com.token.service.UserService;
import com.token.util.sms.EhcacheUtil;
import com.token.util.sms.SmsHuiMark;
import com.token.util.sms.SmsHuiUtil;
import com.token.util.sms.SmsMark;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 用户注册表 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-07-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public ResultPage<User> selectMyPage(VoPage voPage, String status) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("status", status);
        List<User> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<User> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<Jin> selectByStatus(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<Jin> list = baseMapper.selectByStatus(paramMap);
        Integer count = baseMapper.countByStatus(paramMap);
        ResultPage<Jin> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @param tel     手机号
     * @param extractionCode 提取码
     * @return 申请
     */
    @Override
    public String SendSMSO(String tel, String extractionCode) {
        SendSmsResponse response;
        String code = extractionCode;
        try {
            response = SmsHuiUtil.sendSms(tel + "", SmsHuiMark.SMS_MARK, code);
        } catch (ClientException e) {
            e.printStackTrace();
            return "服务器出错，请稍后再试";
        }
        //验证码获取成功后
        if (response.getMessage().equals("OK")) {
            //存进cache缓存  180后过期  当访问过一次后 60秒内没有再此访问 同样过期 缓存区的key设置成数字加字符串 否则获取不到
            EhcacheUtil.put("SMS", tel + "Data", code);
            System.out.println("用户手机号: " + tel);
            System.out.println("用户输入的验证码: " + code);
            System.err.println("----------------------------------");
            return "提交成功";
        }
        return "提交失败";
    }

    //解绑
    @Override
    public Integer updateTel(Long id) {
        return baseMapper.updateTel(id);
    }

    @Override
    public Integer updateByTelStatus(String telStatus) {
        return baseMapper.updateByTelStatus(telStatus);
    }

    @Override
    public Integer qi(String telStatus) {
        return baseMapper.qi(telStatus);
    }

    @Override
    public List<User> selectTel() {
        return baseMapper.selectTel();
    }


}